//
//  TopicCollectionViewCell.m
//  美食百科
//
//  Created by dllo on 16/1/30.
//  Copyright © 2016年 doll. All rights reserved.
//

#import "TopicCollectionViewCell.h"
#import "CarouselModel.h"
#import "UIImageView+WebCache.h"

@interface TopicCollectionViewCell()<UIScrollViewDelegate>
@property(nonatomic,retain)UIImageView *imageView;
@property(nonatomic,retain)UILabel *titleLabel;
@property(nonatomic,retain)UILabel *contentLabel;
@property(nonatomic,retain)UIButton *button;
@property(nonatomic,retain)UIScrollView *scrol;

@property(nonatomic,assign)CGFloat height;
@property(nonatomic,retain)CarouselModel *carouse;
@end
@implementation TopicCollectionViewCell
- (void)dealloc
{[_carousel release];
    [_imageView release];
    [_titleLabel release];
    [_contentLabel release];
    [_scrol release];
    [_button release];
    [_carouse release];
    [super dealloc];
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createScrol];
        
        
        
        
    }
    return self;
}

-(void)createScrol
{
    self.scrol  = [[UIScrollView alloc]init];
    self.scrol.delegate = self;
    self.scrol.bounces = NO;
    self.scrol.showsVerticalScrollIndicator = NO;
    self.imageView = [[UIImageView alloc]init];
   // self.imageView.backgroundColor = [UIColor orangeColor];
//    [self.imageView sd_setImageWithURL:[NSURL URLWithString:self.carousel.image_url]];
    [self.scrol addSubview:self.imageView];
    [_imageView release];
    
    self.titleLabel = [[UILabel alloc]init];
   // self.titleLabel.backgroundColor = [UIColor blueColor];
    self.titleLabel.font = [UIFont systemFontOfSize:23];
    
    self.titleLabel.textColor = [UIColor redColor];
    [self.scrol addSubview:self.titleLabel];
    [_titleLabel release];
    
    self.contentLabel = [[UILabel alloc]init];
   // self.contentLabel.backgroundColor = [UIColor orangeColor];
    self.contentLabel.numberOfLines = 0;
    self.contentLabel.textColor = self.color;

    [self.scrol addSubview:self.contentLabel];
    [_contentLabel release];
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
//    self.button.layer.cornerRadius = 3;
//    self.button.layer.masksToBounds = YES;
    
    [self.button setTitle:@"查看详情" forState:UIControlStateNormal];
    self.button.backgroundColor =[UIColor lightGrayColor];//[UIColor colorWithRed:0.757 green:0.757 blue:0.757 alpha:1.00];
    
    [self.scrol addSubview:self.button];
    
    [self.button addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside ];
    [self.contentView addSubview:self.scrol];
    [_scrol release];
}


-(void)clickAction:(UIButton *)button
{
    self.block(self.carouse);
}

-(void)layoutSubviews
{
    [super layoutSubviews];

    
    
}

+(CGFloat)heightOfSelfSuit:(NSString *)text font:(UIFont *)font width:(CGFloat)width
{
    
    //第一步：设置文本显示的范围
    //如果计算label的自适应高度，那么宽度需要固定，高度需要尽量设置大值；
    
    CGSize size = CGSizeMake(width, MAXFLOAT);
    
    //第二步：设置要显示的文本样式
    NSDictionary *style = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
    
    //第三步：根据文本内容和文本样式计算label的frame
    /**
     *  参数说明：1.文本显示范围；2.文本显示规则；3.文本样式（通常只包括字体）4.通常nil.
     */
    CGRect result = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:style context:nil];
    
    //返回自适应之后的高度
    
    
        return result.size.height;
        
}

-(void)setCarousel:(CarouselModel *)carousel
{
    if (_carousel != carousel) {
        [_carousel release];
        _carousel  = [carousel retain];
    }
    
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:carousel.image_url]placeholderImage:[UIImage imageNamed:@"u=1023456699,842778947&fm=21&gp=0"]];
    
    self.titleLabel.text = carousel.food_name;
    
    self.contentLabel.text = carousel.content;
    self.scrol.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
    
    self.imageView.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height/5*2);
    
    
    self.titleLabel.frame = CGRectMake(20,self.imageView.frame.origin.y + self.imageView.frame.size.height, self.bounds.size.width-40, 60);
    
  
    self.height = [TopicCollectionViewCell heightOfSelfSuit:carousel.content font:[UIFont systemFontOfSize:17] width:self.bounds.size.width -40];
    self.contentLabel.frame = CGRectMake(self.titleLabel.frame.origin.x, self.titleLabel.frame.origin.y+self.titleLabel.frame.size.height, self.titleLabel.frame.size.width, self.height);
    
    
    self.button.frame = CGRectMake(self.bounds.size.width/3, self.contentLabel.frame.origin.y+self.contentLabel.frame.size.height+20, self.bounds.size.width/3, 30);
    
    self.scrol.contentSize = CGSizeMake(0,self.button.frame.origin.y+self.button.frame.size.height+60);
    
    self.carouse = carousel;
}
@end
